﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.IO.Pipes;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinControlService
{
    public partial class ServiceManagementUI : Form
    {
        public ServiceManagementUI()
        {
            InitializeComponent();
            Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath);
        }

        private void btnInstallService_Click(object sender, EventArgs e)
        {
            try
            {
                Dictionary<object, object> savedState = new Dictionary<object, object>();
                using (AssemblyInstaller serviceInstaller = new AssemblyInstaller())
                {
                    serviceInstaller.UseNewContext = true;
                   
                    serviceInstaller.Path = Application.ExecutablePath;
                    serviceInstaller.Install(savedState);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            loadServiceStatus();
        }

        private void btnDeleteService_Click(object sender, EventArgs e)
        {
            Dictionary<object, object> savedState = new Dictionary<object, object>();
            try
            {
                using (AssemblyInstaller serviceInstaller = new AssemblyInstaller())
                {
                    serviceInstaller.UseNewContext = true;
                    serviceInstaller.Path = Application.ExecutablePath;
                    serviceInstaller.Uninstall(savedState);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            loadServiceStatus();
        }

        private void btnStartService_Click(object sender, EventArgs e)
        {
            
            try
            {
                using (ServiceController control = new ServiceController(GPDWin4GameBarPowerBackend.Program.serviceName))
                {
                    if (control.Status == ServiceControllerStatus.Stopped)
                    {
                        control.Start();
                        targetState = 1;
                        stateRefresh.Start();
                    }
                    
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            loadServiceStatus();
        }

        private void btnStopService_Click(object sender, EventArgs e)
        {
            try
            {
                using (ServiceController control = new ServiceController(GPDWin4GameBarPowerBackend.Program.serviceName))
                {
                    if (control.Status == ServiceControllerStatus.Running)
                    {
                        control.Stop();

                        targetState = 0;
                        stateRefresh.Start();
                    }
                    
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            loadServiceStatus();
        }

        
        private void ServiceManagementUI_Load(object sender, EventArgs e)
        {
            loadServiceStatus();
        }

        private void loadServiceStatus()
        {
            try
            {
                using (ServiceController control = new ServiceController(GPDWin4GameBarPowerBackend.Program.serviceName))
                {
                    if (control.Status == ServiceControllerStatus.Running)
                    {
                        lblServiceStatus.Text = "当前服务状态：正在运行";
                        if(targetState == 1)
                        {
                            stateRefresh.Stop();
                        }
                    }
                    else
                    {
                        lblServiceStatus.Text = "当前服务状态：已停止"; 
                        if (targetState == 0)
                        {
                            stateRefresh.Stop();
                        }
                    }
                }
            }
            catch(Exception ex)
            {
                lblServiceStatus.Text = "当前服务状态：未安装";
                if (targetState == -1)
                {
                    stateRefresh.Stop();
                }
            }

        }

        private void btnOpenScriptDir_Click(object sender, EventArgs e)
        {
            string dir = Path.Combine(Application.StartupPath, "bins");
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            Process.Start("explorer.exe",$"\"{dir}\"");
        }

        


        private int targetState = 0; // 1 = on, 0 = 0ff, -1 = not installed
        private void stateRefresh_Tick(object sender, EventArgs e)
        {
            loadServiceStatus();
        }
    }
}
